package com.levi.completableFuture;

import java.util.concurrent.*;

/**
 * @author shentong
 * @since 2022/1/16 4:05 PM
 * 使用supplyAsync创建带返回值的异步任务
 */
public class SupplyAsyncDemo {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName());
            return 123;
        }, executorService);
        System.out.println("Main End~");
        try {
            Integer integer = integerCompletableFuture.get();
            System.out.println(integer);
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
        executorService.shutdown();
    }
}
